Page({
  data: {
    username: '',
    password: ''
  },
  usernameInput: function(e) {
    this.setData({
      username: e.detail.value
    });
  },
  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    });
  },
  login: function(e) {
    const username = this.data.username;
    const password = this.data.password;
    wx.request({
      url: 'http://localhost:8080/login/list',
      header: {
        "content-type": "application/json;charset=UTF-8"
      },
      data: {
        username: username,
        password: password
      },
      method: 'POST',
      success: function(res) {
        if (res.data.code === 200) {
          wx.setStorageSync('username', username);
          wx.setStorageSync('userId', res.data.data.id);
          wx.switchTab({
            url: '/pages/myHome/myHome',
          });
        } else {
          wx.showToast({
            title: res.data.msg || '登录失败',
            icon: 'none'
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '登陆失败',
          icon: 'none'
        });
      }
    });
  }
});